<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@include file="/common.jsp"%>
<!DOCTYPE html>
<html lang="zh">
<meta charset="utf-8">
<head></head>
<body class="white-bg">
	<div class="wrapper wrapper-content animated fadeInRight ibox-content">
		<form class="form-horizontal m" id="form-role-edit">
			<input class="form-control" type="hidden" name="roleSort" id="roleSort" value="1">
			<input id="roleId" name="roleId" type="hidden" value="${role.roleId}" />
			<div class="form-group">
				<label class="col-sm-3 control-label ">角色名称：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="roleName" id="roleName" value="${role.roleName}" />
				</div>
			</div>

			<div class="form-group">
				<label class="col-sm-3 control-label">审核权限：</label>
				<div class="col-sm-8">
					<div class="onoffswitch">
						<input class="onoffswitch-checkbox" ${role.roleKey =='on'?'checked':''} type="checkbox" name="roleKey" id="roleKey">
						<label class="onoffswitch-label" for="roleKey">
							<span class="onoffswitch-inner"></span>
							<span class="onoffswitch-switch"></span>
						</label>
					</div>
				</div>
			</div>

			<%--<div class="form-group">--%>
			<%--<label class="col-sm-3 control-label">显示顺序：</label>--%>
			<%--<div class="col-sm-8">--%>
			<%--<input class="form-control" type="text" name="roleSort" id="roleSort" value="${role.roleSort}">--%>
			<%--</div>--%>
			<%--</div>--%>

			<div class="form-group">
				<label class="col-sm-3 control-label">菜单权限</label>
				<div class="col-sm-8">
					<div id="menuTrees" class="ztree"></div>
				</div>
			</div>

		</form>
	</div>
	<script type="text/javascript">
		$(function() {
			var url = "${ctx}/system/menu/roleMenuTreeData?roleId=" + $("#roleId").val();
			var options = {
				id : "menuTrees",
				url : url,
				check : {
					enable : true,
					nocheckInherit : true,
					chkboxType : {
						"Y" : "ps",
						"N" : "ps"
					}
				},
				expandLevel : 0
			};
			$.tree.init(options);
		});
	
		$("#form-role-edit").validate({
			rules : {
				roleName : {
					required : true,
					remote : {
						url : "${ctx}/system/role/checkRoleNameUnique",
						type : "post",
						dataType : "json",
						data : {
							"roleId" : function() {
								return $("#roleId").val();
							},
							"roleName" : function() {
								return $.common.trim($("#roleName").val());
							}
						},
						dataFilter : function(data, type) {
							return $.validate.unique(data);
						}
					}
				},
				roleKey : {
					required : true,
					remote : {
						url : "${ctx}/system/role/checkRoleKeyUnique",
						type : "post",
						dataType : "json",
						data : {
							"roleId" : function() {
								return $("#roleId").val();
							},
							"roleKey" : function() {
								return $.common.trim($("#roleKey").val());
							}
						},
						dataFilter : function(data, type) {
							return $.validate.unique(data);
						}
					}
				},
				roleSort : {
					required : true,
					digits : true
				},
			},
			messages : {
				"roleName" : {
					remote : "角色名称已经存在"
				},
				"roleKey" : {
					remote : "角色权限已经存在"
				}
			}
		});
	
		function edit() {
			var roleId = $("input[name='roleId']").val();
			var roleName = $("input[name='roleName']").val();
			var roleKey = $("input[name='roleKey']").val();
			var roleSort = $("input[name='roleSort']").val();
			var status = $("input[name='status']").is(':checked') == true ? 0 : 1;
			var remark = $("input[name='remark']").val();
			var menuIds = $.tree.getCheckedNodes();
			$.ajax({
				cache : true,
				type : "POST",
				url : "${ctx}/system/role/edit",
				data : {
					"roleId" : roleId,
					"roleName" : roleName,
					"roleKey" : roleKey,
					"roleSort" : roleSort,
					"status" : status,
					"remark" : remark,
					"menuIds" : menuIds
				},
				async : false,
				error : function(request) {
					$.modal.alertError("系统错误");
				},
				success : function(data) {
					$.operate.successCallback(data);
				}
			});
		}
	
		function submitHandler() {
			if ($.validate.form()) {
				edit();
			}
		}
	</script>
</body>
</html>
